package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.MenuDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.Menu;
import com.zoshine.zzg.pojo.pojostatus.MenuStatus;
import com.zoshine.zzg.util.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 菜单服务实现
 *
 * @author 贾俊峰
 * @time 2017/6/20 15:25
 */
@Service
@Transactional
public class MenuService extends BaseService<Menu> {
    private IdWorker<Long> menuIdWorker;
    private MenuDao menuDao;
    @Autowired
    public void setMenuIdWorker(IdWorker<Long> menuIdWorker) {
        this.menuIdWorker = menuIdWorker;
    }
    @Autowired
    public void setMenuDao(MenuDao menuDao) {
        this.menuDao = menuDao;
    }

    @Override
    public Menu add(Menu menu) {
        // 非空验证
        if (menu.getMenuId() == null) {
            throw new AppException(ErrorMessage.MENU_ID_CAN_NOT_BE_NULL);
        }
        if (menu.getMenuName() == null) {
            throw new AppException(ErrorMessage.MENU_NAME_CAN_NOT_BE_NULL);
        }
        // 值验证
        if (menu.getMenuId().length() != 6) {
            throw new AppException(ErrorMessage.MENU_ID_MUST_BE_6_NUMBER);
        }
        if (menu.getMenuName().length() > 20) {
            throw new AppException(ErrorMessage.MENU_NAME_LENGTH_CAN_NOT_OVER_20);
        }
        if (menu.getMenuIcon() != null && menu.getMenuIcon().length() > 60) {
            throw new AppException(ErrorMessage.MENU_ICON_LENGTH_CAN_NOT_OVER_60);
        }

        // 验证menuId非重复
        Menu check = new Menu();
        check.setMenuId(menu.getMenuId());
        check = menuDao.selectOne(check);
        if (check != null) {
            throw new AppException(ErrorMessage.MENU_ID_ALREADY_EXIST);
        }

        // 补全信息
        menu.setId(menuIdWorker.createId());
        if (menu.getMenuStatus() == null) {
            menu.setMenuStatus(MenuStatus.HIDE.getValue());
        }
        completeMenuLevel(menu);
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        menuDao.insert(menu);
        return menu;
    }

//    @Override
//    public Menu getMenu(Long pid) {
//        return menuDao.selectOne(pid);
//    }

//    @Override
    public List<Menu> listTopMenu() {
        Menu menu = new Menu();
        menu.setMenuLevel(1);
        menu.setMenuStatus(MenuStatus.SHOW.getValue());
        return menuDao.selectList(menu);
    }

    public List<Menu> listSubMenu(String parentId) {
        Menu menu = new Menu();
        menu.setParentId(parentId);
        menu.setMenuStatus(MenuStatus.SHOW.getValue());
        return menuDao.selectList(menu);
    }
//
//    @Override
//    public List<Menu> listMenuByParentId(String parentId) {
//        Menu menu = new Menu();
//        menu.setMenuStatus(MenuStatus.SHOW.getValue());
//        menu.setParentId(parentId);
//        return menuDao.selectList(menu);
//    }

    @Override
    public int edit(Menu menu) {
        if (menu.getId() == null) {
            throw new AppException(ErrorMessage.PID_CAN_NOT_BE_NULL);
        }
        // 非空验证
        if (menu.getMenuId() == null) {
            throw new AppException(ErrorMessage.MENU_ID_CAN_NOT_BE_NULL);
        }
        if (menu.getMenuName() == null) {
            throw new AppException(ErrorMessage.MENU_NAME_CAN_NOT_BE_NULL);
        }
        if (menu.getMenuStatus() == null) {
            throw new AppException(ErrorMessage.MENU_STATUS_CAN_NOT_BE_NULL);
        }
        // 值验证
        if (menu.getMenuId().length() != 6) {
            throw new AppException(ErrorMessage.MENU_ID_MUST_BE_6_NUMBER);
        }
        if (menu.getMenuName().length() > 20) {
            throw new AppException(ErrorMessage.MENU_NAME_LENGTH_CAN_NOT_OVER_20);
        }
        if (menu.getMenuIcon() != null && menu.getMenuIcon().length() > 60) {
            throw new AppException(ErrorMessage.MENU_ICON_LENGTH_CAN_NOT_OVER_60);
        }
        // 验证menuId非重复
        Menu check = menuDao.selectByMenuId(menu.getMenuId());
        if (check != null && !Objects.equals(check.getId(), menu.getId())) {
            throw new AppException(ErrorMessage.MENU_ID_ALREADY_EXIST);
        }
        completeMenuLevel(menu);
        menu.setUpdateTime(new Date());
        return menuDao.update(menu);
    }

//    @Override
//    public void delMenu(Long pid) {
//        if (pid == null) {
//            throw new AppException(ErrorMessage.MENU_ID_CAN_NOT_BE_NULL);
//        }
//        menuDao.delete(pid);
//    }

//    @Override
//    public List<Menu> listMenu(Menu menu) {
//        return menuDao.selectList(menu);
//    }
//
//    @Override
//    public void batchDelMenu(String pidStr) {
//        if (pidStr == null || pidStr.length()==0) {
//            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
//        }
//        String[] pidArr = pidStr.split(",");
//        List<Long> pidList = new ArrayList<>();
//        for (String pid : pidArr) {
//            try {
//                Long id = Long.parseLong(pid);
//                pidList.add(id);
//            } catch (NumberFormatException ex) {
//                throw new AppException(ErrorMessage.REQUEST_PARAM_TYPE_ERROR);
//            }
//        }
//        if (pidList.size() != 0) {
//            menuDao.batchDelete(pidList);
//        }
//    }

    /**
     * 补全菜单级别
     * @param menu 菜单信息
     */
    private void completeMenuLevel(Menu menu) {
        if (!Objects.equals(menu.getParentId(), "0")) {
            Menu parent = menuDao.selectByMenuId(menu.getParentId());
            if (parent == null) {
                throw new AppException(ErrorMessage.PARENT_MENU_NOT_EXIST);
            }
            menu.setMenuLevel(parent.getMenuLevel() + 1);
        } else {
            menu.setMenuLevel(1);
        }
    }
}
